#include "Tracks.h"
#include <math.h>

GLfloat tracks_diffuse[] = {0.7, 0.7, 0.7, 1.0};
GLfloat tracks_ambient[] = {0.7, 0.7, 0.7, 1.0};
GLfloat tracks_specular[] = {0.5, 0.5, 0.5, 1.0};

// Shininess
GLfloat tracks_shininess = 10.0f;

Tracks::Tracks(void)
{
	heightCenterPieceT = 0.3;
	heightTopPieceT = 0.1;
	dimensionsWheelsT = 0.15;
	quad = gluNewQuadric();
	loadImage();
}

void Tracks::loadImage()
{
	Image* image1 = loadBMP("textures\\textureBipod.bmp");
	Image* image2 = loadBMP("textures\\textureBipod2.bmp");
	Image* image3 = loadBMP("textures\\textureCannon.bmp");
	Image* image4 = loadBMP("textures\\wheelsTracks.bmp");
	Image* image5 = loadBMP("textures\\textureAlu.bmp");
	Image* image6 = loadBMP("textures\\textureRusty.bmp");
	textureBase1 = loadTexture(image1);
	textureBase2 = loadTexture(image2);
	textureBase3 = loadTexture(image5);
	textureBase4 = loadTexture(image6);;
	textureWheels1 = loadTexture(image3);
	textureWheels2 = loadTexture(image4);

	currentTextureBase = textureBase1;
	currentTextureBase2 = textureBase3;
	
	delete image1;
	delete image2;
	delete image3;
	delete image4;
	delete image5;
	delete image6;
}

void Tracks::switchTexture()
{
	if(currentTextureBase == textureBase1)
		currentTextureBase = textureBase2;
	else
		currentTextureBase = textureBase1;
	if(currentTextureBase2 == textureBase3)
		currentTextureBase2 = textureBase4;
	else
		currentTextureBase2 = textureBase3;
}

void Tracks::display()
{
	glPushMatrix();
		glTranslatef(0.0, elevation, 0.0);
		glDisable(GL_CULL_FACE);
		glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, tracks_diffuse);
		glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, tracks_ambient );
		glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, tracks_specular);
		glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, tracks_shininess);
		
		gluQuadricNormals(quad, GLU_SMOOTH);   // Create Smooth Normals (better for light)
		gluQuadricTexture(quad, GL_TRUE);      // Create Texture Coords

		glColor3f(1,1,1);
		glTranslatef(0, dimensionsWheelsT, 0);
		
		glBindTexture(GL_TEXTURE_2D, currentTextureBase2);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glPushMatrix();
			glTranslatef(0, 0.15, 0);
			glScalef(1.0, heightCenterPieceT, 0.75);
			drawCube();
		glPopMatrix();

		//Wheels Right
		glPushMatrix();
			glTranslatef(0.5, 0, -0.6);
			glScalef(dimensionsWheelsT, dimensionsWheelsT, dimensionsWheelsT);
			glRotatef(90, 0, 1, 0);
			for(int i = 0; i<3; ++i)
			{
				glTranslatef(-2.0, 0.0, 0.0);
				createTube();
			}	
		glPopMatrix();

		//Wheels Left
		glPushMatrix();
			glTranslatef(-0.65, 0, -0.6);
			glScalef(dimensionsWheelsT, dimensionsWheelsT, dimensionsWheelsT);
			glRotatef(90, 0, 1, 0);
			for(int i = 0; i<3; ++i)
			{
				glTranslatef(-2.0, 0.0, 0.0);
				createTube();
			}	
		glPopMatrix();

		glBindTexture(GL_TEXTURE_2D, currentTextureBase);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		//TOP
		glPushMatrix();
			glTranslatef(0.0, 0.335, 0.0);
			glPushMatrix();
				glScalef(1.0, heightTopPieceT, 1.0);
				drawCube(0.5,0.55);
			glPopMatrix();
		glPopMatrix();
	glPopMatrix();
	
}

GLuint Tracks::getTexture()
{
	return currentTextureBase2;
}

void Tracks::createTube()
{	
	glPushMatrix();
		glBindTexture(GL_TEXTURE_2D, textureWheels1);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		gluCylinder(quad, 1.0, 1.0, 1.0, 30.0, 3.0);
	glPopMatrix();
	// Caps
	glPushMatrix();
		glBindTexture(GL_TEXTURE_2D, textureWheels2);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		gluDisk(quad, 0.0, 1.0, 30.0, 3.0);
	glPopMatrix();

	glPushMatrix();
		glTranslatef(0.0, 0.0, 1.0);
		gluDisk(quad, 0.0, 1.0, 30.0, 3.0);
	glPopMatrix();
}

float Tracks::getHeight()
{
	return dimensionsWheelsT + heightCenterPieceT + heightTopPieceT;
}

Tracks::~Tracks(void)
{
	gluDeleteQuadric(quad);
}
